package com.canyou.support;

import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * RequestParam 参数解析 ZonedDateTime
 *
 * @author fy
 * @date 20/09/04 10:18
 */
@Component
public class ZonedDateTimeFormatter implements Formatter<ZonedDateTime> {
    @Override
    public ZonedDateTime parse(String text, Locale locale) throws ParseException {
        return ZonedDateTime.parse(text).withZoneSameInstant(ZoneId.systemDefault());
    }

    @Override
    public String print(ZonedDateTime object, Locale locale) {
        return object.format(DateTimeFormatter.ISO_DATE_TIME);
    }
}
